package com.example.sixtrainingdemo.view;

import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.base.BaseFragment;
import com.example.sixtrainingdemo.contract.MineContract;
import com.example.sixtrainingdemo.entity.User;
import com.example.sixtrainingdemo.presenter.MinePresenter;
import com.example.sixtrainingdemo.utils.SpUtils;

public class MineFragment extends BaseFragment implements View.OnClickListener, MineContract.IMineView {

   private TextView mTvName;
   private RelativeLayout mSettingLayout;

   private RelativeLayout mOtherLayout;
   private MineContract.IMinePresenter mMinePresenter;
   private SpUtils mSpUtils;
   @Override
   protected int getLayoutId() {
      return R.layout.fragment_mine;
   }

   @Override
   protected void initView() {
      mMinePresenter = new MinePresenter(this, getContext());
      mSettingLayout = mView.findViewById(R.id.settingView);
      mOtherLayout = mView.findViewById(R.id.otherView);
      mTvName = mView.findViewById(R.id.user_name);
      mSpUtils = new SpUtils(mContext);
   }

   @Override
   public void onResume() {
      super.onResume();
      //查询当前登录帐号的id
      int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, 1);//目前无登录功能，用默认值1作为登录id。
      if (loginId >= 0) {
         //根据id查询当前登录用户的信息
         mMinePresenter.getLoginUser(loginId);
      }
   }

   @Override
   protected void initListener() {
      mSettingLayout.setOnClickListener(this);
      mOtherLayout.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.settingView) {
         Intent intent = new Intent(mContext, SettingsActivity.class);
         startActivity(intent);
      }else if (id == R.id.otherView) {
         Intent intent = new Intent(mContext, OtherActivity.class);
         startActivity(intent);
      }
   }

   @Override
   public void showUserInfo(User user) {
      if (mTvName != null && user != null) {
         mTvName.setText(user.getUserName());
      }
   }
}
